package cn.bugstack.domain.activity.service.discount.impl;

import cn.bugstack.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.bugstack.domain.activity.service.discount.AbstractDiscountCalculateService;
import cn.bugstack.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author yjn
 * @description 满减计算服务
 * @create 2025/8/11 21:13
 */
@Slf4j
@Service("MJ")
public class MJCalculateService extends AbstractDiscountCalculateService {
    @Override
    protected BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        log.info("优惠策略折扣计算:{}", groupBuyDiscount.getDiscountType().getCode());

        //折扣表达式 - 100,10 满100减10；
        String marketExpr = groupBuyDiscount.getMarketExpr();
        String[] split = marketExpr.split(Constants.SPLIT);
        BigDecimal x = new BigDecimal(split[0].trim());
        BigDecimal y = new BigDecimal(split[1].trim());

        //不满足满减要求
        if(originalPrice.compareTo(x) < 0)
            return originalPrice;

        //折扣价
        BigDecimal deductionPrice = originalPrice.subtract(y);

        ///最低支付1分钱
        if(deductionPrice.compareTo(BigDecimal.ZERO)<=0)
            return new BigDecimal("0.01");

        return deductionPrice;
    }
}
